<template>
<div style="position:relative; overflow:auto" >
    <div id="content" >
		<p>
			<button class="btn btn-info" @click="handleRecord = true;operation='add'">新建</button>
		</p>
		<div id="table">
			<table>
				<thead>
					<tr>
                        <th>NVR名称</th>
						<th>IP地址 </th>
						<th>用户名 </th>
						<th>密码   </th>
						<th>可用视屏通道</th>
						<th>编辑</td>
						<th>删除</th>
					</tr>
				</thead>
				<tbody>
                    <tr v-for="(item,ip) in trs">
						<td>{{item.name}}</td>
						<td>{{item.ip}}</td>
						<td>{{item.username}}</td>
						<td>{{item.password}}</td>
						<td>
							<div v-if="item.channels" >
								{{ item.channels }}
							</div>
							<div v-else  style="display:inline-block;" >
								<svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewBox="0 0 231 231.1" class="spin-waiting-channels"> <g id="图层_2" data-name="图层 2"><g id="图层_2-2" data-name="图层 2"><path class="cls-1" d="M231,115.6A115.64,115.64,0,0,1,115.5,231.1a10,10,0,0,1,0-20A95.61,95.61,0,0,0,211,115.6a10,10,0,0,1,20,0Z"/><path class="cls-1" d="M125.5,10.1a10,10,0,0,1-10,10h-1.21A95.6,95.6,0,0,0,20,115.6a10,10,0,0,1-20,0A115.62,115.62,0,0,1,114,.12c.51,0,1,0,1.54,0A10,10,0,0,1,125.5,10.1Z"/><path class="cls-1" d="M125.59,10.1A10.09,10.09,0,0,1,115.5,20.19a8.11,8.11,0,0,1-1.21-.08h1.21a10,10,0,1,0,0-20c-.51,0-1,0-1.54,0A9.8,9.8,0,0,1,115.5,0,10.1,10.1,0,0,1,125.59,10.1Z"/><circle class="cls-1" cx="142.8" cy="13.69" r="10.09" transform="translate(92.62 148.08) rotate(-75)"/><circle class="cls-1" cx="88.19" cy="217.5" r="10.09" transform="translate(-144.72 246.39) rotate(-75)"/></g></g></svg>
							</div>
						</td>

						<td><button class="btn btn-info btn-sm" @click="edit(ip)">编辑</button></td>
						<td><button class="btn btn-info btn-sm" @click="cut(ip)">删除</button></td>
					</tr>
				</tbody>
			</table>
		</div>
		<div id="handleRecord" v-show="handleRecord">
			<p> {{operation =='add'?  '新建区域' : '编辑区域'}}</p>
			<label>NVR名称</label>
			<input type="text" placeholder="NVR名称(必填)" v-model="name">	
			<label>IP地址</label>
			<input type="text" placeholder="IP地址(输入合法IP地址)" v-model="ip">
			<label>用户名</label>
			<input type="text" placeholder="用户名(默认admin)" v-model="username">	
			<label>密码</label>
			<input type="text" placeholder="密码" v-model="password">	
			<div id="btn">
				<button  class="btn btn-info" @click="submit()" :disabled='!legal'>提交</button>
				<button  class="btn btn-info" @click="handleRecord = false">返回</button>
			</div>
		</div>
	</div>
	<div id="dummy-video" style="display:block">
	</div>


    <div id="footer">
        <router-link tag="button" to="selectProtector" class="btn btn-primary" @click.native="$store.commit('setStep',2)"> 上一步</router-link>
        <router-link tag="button" to="layoutPic" class="btn btn-primary" @click.native="$store.commit('setStep',4)" :disabled="trs.length<=0"> 下一步</router-link>  
    </div>
</div>
</template>


<script>
import { initHikvision, clickLogin, clickGetDigitalChannelInfo  } from '@/common/hikvision'

export default {
    data(){
        return{
            handleRecord : false,
			name: '',
			ip : '',
			username: 'admin',
			password: '',
			operation: '',
        }
	},
	mounted(){
		initHikvision( "dummy-video", 300, 180 );
	},
	computed:{
		legal(){
			var IpReg = /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
			let ok = true;
			if( !this.username || !this.password  )
				ok = false;

			ok =  ok && this.name && IpReg.test(this.ip);
			return ok;
		},
		trs(){
			return this.$store.state.nvrs;
		}
	},
	methods: {
		submit(){
			let vm = this;
			if(this.operation == 'add'){
				console.log( "add nvr: ", vm.ip )
				//貌似直接push,splice,$set等操作可以直接修改到state里面的值，不需要使用commit
				vm.$set( vm.trs, vm.ip, { 
					ip: vm.ip,
					name: vm.name,
					username: vm.username,
					password: vm.password
				});
				console.log( vm.trs );
			}
			if(this.operation == 'edit'){
				console.log( "edit nvr: ", vm.ip )
				vm.$delete( vm.trs, vm.ip );
				vm.$set( vm.trs, vm.ip, {
					ip: vm.ip,
					name: vm.name,
					username: vm.username,
					password: vm.password
				});
			}
			this.handleRecord = false;
			clickLogin( this.ip, 80, vm.username, vm.password ).then( function( data ){ 
				clickGetDigitalChannelInfo( vm.ip, 80 ).then( function( channelInfo ) {
    				console.log( channelInfo );
    				var channels = [];
    				for( let i = 0; i < channelInfo.length; i++ ){
    					if( channelInfo[i].online =='true' ) { 
							console.log( 'get online channel ');
    						channels.push( channelInfo[i].id );
    					}
					}
    				vm.$set( vm.trs[vm.ip], 'channels', channels );

				})
			});
		},
		cut( ip ){
			let vm = this;
			vm.$delete( vm.trs, ip );
		},
		edit(ip){
			this.handleRecord = true;
			this.operation = 'edit';

			this.ip = this.trs[ip].ip;
			this.name = this.trs[ip].name;
			this.username = this.trs[ip].username;
			this.password = this.trs[ip].password;
		}
	}
}
</script>



<style scoped>
#content{
	float: left;
    height: 600px;
	width: 80%;
	margin:0px;
}
#dummy-video{
	float: right;
	width: 20%;
	top:0px;
}

#table{
    margin-left: 30px;
    margin-top: 50px;
    width: 800px;
    border: 1px solid #e1e1e1; 
}
div p button{
	width: 80px;
	height: 30px;
	margin-left: 40px;
	margin-top: 30px;
}
#content th{
    background-color: #f2f2f2;
}
#content th:nth-child(1){
	width: 240px;
	text-align: center;
}
#content th:nth-child(2){
	width: 200px;
	text-align: center;
}
#content th:nth-child(3){
	width: 50px;
	text-align: center;
}
#content th:nth-child(4){
	width: 60px;
	text-align: center;
}
#content td,#content th{
    border-left: 1px solid #e1e1e1;
	text-align: center;
	height: 50px;
}

#handleRecord{
	border:1px solid #f1f1f1;
	background-color: #f1f1f1;
	margin: 0 auto;
	width: 500px;
	z-index: 2;
	position: absolute;
	top: 250px;
	left: 0px;
	right: 0px;
}

#handleRecord p{
	height: 30px;
	line-height: 30px;
	font-size: 22px;
	background-color: #0087da;
    border-color: #0087da;
	color: white;
	text-align: center;
}

#handleRecord label{
	display: inline-block;
	height: 40px;
	line-height: 40px;
	width: 30%;
	text-align: center;
}

#handleRecord input{
	display: inline-block;
	height: 40px;
	width: 60%;
}
#handleRecord button{
	display: inline-block;
	background-color: #0087da;
    border-color: #0087da;
	border-radius: 8px;
	width: 100px;
	height: 30px;
	margin: 20px;
}
#handleRecord #btn{
	width: 300px;
	margin: 0 auto;
	color:white;
}

#footer{
    margin-left: 30px;
    margin-top: 50px;
    width: 550px;
    text-align: center;
}
#footer button{
    margin: 0px 20px;
}


.spin-waiting-channels {
  animation: rotate 1.4s linear infinite;
  -webkit-animation: rotate 1.4s linear infinite;
  -moz-animation: rotate 1.4s linear infinite;
  width:  30px;
  height: 30px;
  position: relative;
}

@keyframes rotate {
  to {
    transform: rotate(360deg);
  }
}

@-webkit-keyframes rotate {
  to {
    -webkit-transform: rotate(360deg);
  }
}

@-moz-keyframes rotate {
  to {
    transform: rotate(360deg);
  }
}

.cls-1 {
	fill:#7acc7d;
}


</style>

